import string
import time
import CONST
from persistence import sqlUtil
from pypinyin import lazy_pinyin
from bisect import bisect_right

db_name = 'kw_cpt_user'


def user_register(id, password):

    sql1 = 'insert into login(id,pwd) values({}, {})'.format(id, password)
    # sql2 = 'insert into user_info values({}, {},{},{},{},{},{})'.format(id, 'abc', '88888888', '123', 1, 2022 - 11 - 15, 2022 - 11 - 15)

    try:
        conn, cur = sqlUtil.get_cur(db_name)
        print(cur.execute(sql1))
        # cur.execute(sql2)
    except Exception as e:
        conn.rollback()
        print('事务处理失败', e)
    finally:
        sqlUtil.close_db(conn, cur)

def is_over_num(phone):
    sql1 = 'select id from user_info where phone={}'.format(phone)
    # print(sql1)
    sql2 = 'select times from api where id={}'
    try:
        conn, cur = sqlUtil.get_cur(db_name)
        cur.execute(sql1)
        user_id = cur.fetchall()[0][0]
        # print(sql2.format(user_id))
        cur.execute(sql2.format(user_id))
        result = cur.fetchall()[0][0]
        return result
    except Exception as e:
        conn.rollback()
        print('事务处理失败', e)
    finally:
        sqlUtil.close_db(conn, cur)

def add_one_num(phone, num):
    sql1 = 'select id from user_info where phone={}'.format(phone)
    sql2 = 'update api set times={} where id={}'
    try:
        conn, cur = sqlUtil.get_cur(db_name)
        cur.execute(sql1)
        user_id = cur.fetchall()[0][0]
        cur.execute(sql2.format(num, user_id))
        conn.commit()
    except Exception as e:
        conn.rollback()
        print('事务处理失败', e)
    finally:
        sqlUtil.close_db(conn, cur)

if __name__ == '__main__':
    # print(is_over_num(11111))
    print(add_one_num(11111, 1))